__author__ = 'Chenchen'

import os
import os.path
import trans_file
import re


def cast_list_data(cmd_opt, lines):
    new_lines = []
    for line in lines:
        new_line = cmd_opt.trans_line(line)
        new_lines.append(new_line)
    return new_lines


def create_file_by_file_list(target_path, target_name, file, temp_file_lines):
    prj_name = target_name
    target_path = os.path.join(target_path, target_name)
    cmd_opt = trans_file.CmdOperator(prj_name)
    real_file_name = cmd_opt.trans_line(file)

    dst_file_lines = cast_list_data(cmd_opt, temp_file_lines)

    if not os.path.exists(target_path):
        os.makedirs(target_path)

    real_file_path = os.path.join(target_path, real_file_name)
    with open(real_file_path, "wt", encoding="utf-8") as targetFile:
        for l in dst_file_lines:
            targetFile.write(l)
    return


def export_new_project(template_path, target_path, target_name):
    for root, dirs, files in os.walk(template_path):
        for file in files:
            file_path = os.path.join(root, file)
            with open(file_path, "rt", encoding="utf-8") as tempFile:
                temp_file_lines = list(tempFile)
                create_file_by_file_list(target_path, target_name, file, temp_file_lines)


if __name__ == "__main__":
    import sys
    if len(sys.argv) < 2:
        target_path = input("which path need create:")
    else:
        target_path = sys.args[1]
    target_dir, target_name = os.path.split(target_path)
    cur_path = os.path.dirname(os.path.abspath(__file__))
    template_path = os.path.join(cur_path, './template/dll/')
    export_new_project(template_path, target_dir, target_name)